package cn.truistic.enmicromsg.wx.wx_nearby;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;

import cn.truistic.enmicromsg.app.EnApplication;
import cn.truistic.enmicromsg.common.util.IDConstant;
import cn.truistic.enmicromsg.common.util.PreferenceHelper;
import cn.truistic.enmicromsg.common.util.RootUtil;
import cn.truistic.enmicromsg.common.util.WxHeple;

/**
 * Created by admin on 2018/6/19.
 * name: ts
 * 用途: 附近的人的模块
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class WeChatNearbyHeper extends WxHeple {

    /**
     * 附近的人默认内容
     */
    private String NearByContent = "hello 你好";
    /**
     * 当前附近人访问的位置
     */
    private int mNearbyNumber = 0;
    /**
     * 当前附近的访问总人数
     */
    private int mNearbySumNumber = 0;
    /**
     * 当前第几个地点
     */
    public static int index = 0;
    /**
     * 当前界面状态
     */
    private int pageStatus = 0;
    /**
     * 打招呼页面
     */
    public static final int SAY_HI_EDIT_UI = 3;

    private int clickYESNum = 0;
    private int nearbyGreetNum = 0;
    private boolean isAlreadyReply = false;
    private boolean isFrequently = false;
    private final int FRIENDS_VERIFY = 13;
    private final int NEARBY_DETAIL_UI = 14;

    public WeChatNearbyHeper(EnApplication mContext, AccessibilityService mAccessibilityService) {
        super(mContext, mAccessibilityService);
    }

    //进行自动化 ----附近的人打招呼
    public void isAutomationNearby( AccessibilityEvent event, int eventType ) {
        //获取需要打招呼的人数
        int mNearTextNum = PreferenceHelper.getInt("mNearTextNum", 0);
        if (mEnApplication.isAllowGreeting()) {
            Log.e("TAG","22222222222222");
            sleepTime(1000);
            //复制需要输入的评论内容
            String mNearText = PreferenceHelper.getString("ContentDialogText", "");//本地
            if (TextUtils.isEmpty(mNearText)) {
                mNearText = NearByContent;
            }
            copyToBoard(mNearText);

            //如果是主界面，并找到发现按钮
            if (isFindText("发现") && isFindId(IDConstant.MAIN_BUTTON)) {
                Log.e("TAG","33333333333");
                sleepTime(1000);
                //Log.i("890", "node");
                WXfindIdTextAndClick(IDConstant.MAIN_BUTTON , "发现" , 2 );
                //findTextAndClick("发现", 0);  //点击发现
            }
            if ( isFindText("附近的人")) {
                Log.e("TAG","44444444444444");
                sleepTime(1000);
                findTextAndClick("附近的人", 0);  //点击附近的人
                sleepTime(1000);
                if ( isFindId( IDConstant.WX_NO_NEXT ) ) {
                    // 下次不提示
                    findIdAndClick(IDConstant.WX_NO_NEXT , 0);
                    sleepTime(1000);
                    // 确定
                    findIdAndClick(IDConstant.WX_NO_NEXT_YES , 0);
                }
                mNearbyNumber = 0;
                mEnApplication.setAllowGreeting( false );
            }
        }
        /**
         * 附近de人界面
         */
        if ( event.getClassName().equals("com.tencent.mm.plugin.nearby.ui.NearbyFriendsUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED ) {
            Log.e("TAG","5555555555555");
            if ( !isAlreadyReply ) {
                findContentAndClick(IDConstant.WX_HEAD, 0, "更多", 2);
                sleepTime(1000);
                findIdAndClick(IDConstant.WX_GREET_NEARBY, 3);
                isAlreadyReply = true;
            } else {
//                pageStatus = NEARBY_FRIENDS_UI;     //当前在附近人界面
                //当一页添加9人时，就进行翻页操作
                if ( mNearbyNumber >= 9 ) {
                    sleepTime(1500);
                    RootUtil.execSlipCmd("input swipe 400 1200 400 520"); //翻页操作
                    sleepTime(5000);
                    mNearbyNumber = 0;
                }
                sleepTime(1000);
                mNearbySumNumber++ ;
                if ( mNearbySumNumber > mNearTextNum ) {
                    index++;
                    //切换地点
//                   ChangeLocation();
                    // TODO 切换账号
                    //                            MyToast.show(EarnAccessibilityService.this, "服务已完结");
                    mEnApplication.setAllowGreeting(true);
                    mNearbyNumber = 0;
                    mNearbySumNumber = 0;
                } else {
                    findIdAndClick( IDConstant.NEARBY_LIST_VIEW , mNearbyNumber);
                    mNearbyNumber++;
                }
                //当浏览完毕退出，并将附近人模块开启状态变成false
            }
        }

        //附近打招呼的人界面
        else if ( event.getClassName().equals("com.tencent.mm.plugin.nearby.ui.NearbySayHiListUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED ) {
            clickYESNum = 0;
            //是否是频繁过来的
            clickYESNum = 0;
            if (isFrequently) {
                sleepTime(1000);
                findIdAndClick(IDConstant.WX_GREET_NEARBY_BACK, 0);
                isFrequently = false;
            } else {
                //有查看更多按钮时
                if (isFindId(IDConstant.WX_LOOK_MORE)) {
                    sleepTime(1000);
                    findIdAndClick(IDConstant.WX_LOOK_MORE, 0);
                }
                if (nearbyGreetNum >= 9) {
                    nearbyGreetNum = 0;
                    sleepTime(1500);
                    RootUtil.execSlipCmd("input swipe 400 1200 400 520");
                    sleepTime(5000);
                }
                AccessibilityNodeInfo nodeInfo = mAccessibilityService.getRootInActiveWindow();
                List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/" + IDConstant.WX_GREET_NEARBY_LIST);
                Log.i("zzz1", list.size() + "" );
                if ( list.size() == 0 ) {
                    sleepTime(1000);
                    findIdAndClick(IDConstant.WX_GREET_NEARBY_BACK, 0);
                } else if (list.size() < 9 && list.size() > 0) {
                    if (nearbyGreetNum >= list.size()) {
                        sleepTime(1000);
                        findIdAndClick(IDConstant.WX_WX_GREET_NEARBY_CLEAR, 0);
                        sleepTime(1000);
                        findIdAndClick(IDConstant.WX_WX_GREET_NEARBY_CLEAR_YES, 0);
                        sleepTime(1000);
                        //                                    findIdAndClick(IDConstant.WX_GREET_NEARBY_BACK, 0);
                    }
                } else {
                    Log.i("zzz1", isFindWXBouns(IDConstant.WX_GREET_NEARBY_LIST, nearbyGreetNum) + "");
                    if (isFindWXBouns(IDConstant.WX_GREET_NEARBY_LIST, nearbyGreetNum)) {
                        sleepTime(1000);
                        findIdAndClick(IDConstant.WX_WX_GREET_NEARBY_CLEAR, 0);
                        sleepTime(1000);
                        findIdAndClick(IDConstant.WX_WX_GREET_NEARBY_CLEAR_YES, 0);
                        sleepTime(1000);
                        //                                    findIdAndClick(IDConstant.WX_GREET_NEARBY_BACK, 0);
                    }
                }
                Log.i("zzz1" , "mm" );
                findIdAndClick ( IDConstant.WX_GREET_NEARBY_LIST , nearbyGreetNum );
                nearbyGreetNum++;
            }
        }

        //朋友验证界面
        else if (event.getClassName().equals("com.tencent.mm.plugin.profile.ui.SayHiWithSnsPermissionUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            pageStatus = FRIENDS_VERIFY;
            sleepTime(1000);
            findIdAndClick(IDConstant.WX_FRIENDS_VERIFY, 0);
            clickYESNum++;
            Log.i("zzz2", clickYESNum + "");
            if ( clickYESNum >= 3 ) {
                sleepTime(1000);
                findIdAndClick(IDConstant.WX_FRIENDS_SURE_BACK, 0);
                clickYESNum = 0 ;
                isFrequently = false;
                sleepTime(1000);
            }
        } else if (event.getClassName().equals("com.tencent.mm.plugin.nearby.ui.NearbyFriendsIntroUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {//第一次进入附近的人
            //第一次打开微信附近的人会出现开始查看
            findTextAndClick("开始查看", 0);
            sleepTime(1000);
            // 下次不提示
            findIdAndClick(IDConstant.WX_NO_NEXT, 0);
            sleepTime(1000);
            // 确定
            findIdAndClick(IDConstant.WX_NO_NEXT_YES, 0);

        } else if (event.getClassName().equals("com.tencent.mm.plugin.nearby.ui.NearbyFriendShowSayHiUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            if (isFindText("查看附近的人")) {
                WXfindIdTextAndClick(IDConstant.NEARBY_CHECK_BUTTON, "查看附近的人", 0);
                //findTextAndClick("查看附近的人", 0);
            }

            /**
             * 详细资料界面
             */
        } else if (event.getClassName().equals("com.tencent.mm.plugin.profile.ui.ContactInfoUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED ) {
            //附近人打招呼详细资料界面
            sleepTime(1000);
            if (pageStatus == FRIENDS_VERIFY || pageStatus == SAY_HI_EDIT_UI || isFindText("发消息")) {
                pageStatus = NEARBY_DETAIL_UI;
                findIdAndClick(IDConstant.WX_GREET_NEARBY_BACK, 0 );
            } else {
                pageStatus = NEARBY_DETAIL_UI;
                if (isFindText("通过验证")) {
                    findTextAndClick("通过验证", 0);
                } else if (isFindText("打招呼")) {
                    WXfindIdTextAndClick( IDConstant.WX_HELLO_BUTTON, "打招呼", 0);
                } else {
                    RootUtil.execSlipCmd("input swipe 400 1100 400 500");
                    sleepTime(2000);
                    if (isFindText("通过验证")) {
                        findTextAndClick("通过验证", 0);
                    } else if (isFindText("打招呼")) {
                        WXfindIdTextAndClick(IDConstant.WX_HELLO_BUTTON, "打招呼", 0);
                    }
                }
            }
        }
        /**
         * 打招呼界面
         */
        else if (event.getClassName().equals("com.tencent.mm.ui.contact.SayHiEditUI")
                && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            pageStatus = SAY_HI_EDIT_UI;    //当前在打招呼界面
            sleepTime(1000);
            paste(IDConstant.SAY_HELLO_EDIT_TEXT, 0);   // 粘贴进剪切板
            sleepTime(1000);
            //findIdAndClick(IDConstant.WX_HELLO_SEND_BUTTON, 0); // 点击发送
            WXfindIdTextAndClick(IDConstant.WX_HELLO_SEND_BUTTON, "发送", 0);
        }
    }
}
